#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
char arr[N][N];
bool flag[N][N];
int r, c, T, n;
int ans;
int dx[4] = {0, 0, 1, -1}, dy[4] = { 1, -1, 0, 0 };

void bfs(int i, int j)
{
    queue<PII> q;
    q.push({i, j});
    flag[i][j] = true;
    while(q.size())
    {
        auto tt = q.front();
        q.pop();
        int x = tt.first, y = tt.second;
        for(int k = 0; k < 4; ++k)
        {
            int xx = x + dx[k], yy = y + dy[k];
            if(xx > 0 && xx <= r && yy > 0 && yy <= c && arr[xx][yy] == '1' && !flag[xx][yy])
            {
                q.push({xx, yy});
                flag[xx][yy] = true;
            }
        }
    }
}

int init()
{
    memset(flag, 0, sizeof flag);
    ans = 0;
    for(int i = 1; i <= r; ++i)
        for(int j = 1; j <= c; ++j)
            if(arr[i][j] == '1' && !flag[i][j])
                bfs(i, j), ++ans;
    return ans;
}

int main()
{
    cin >> T;
    for(int i = 1; i <= T; ++i)
    {

        cin >> r >> c;
        for(int i = 1; i <= r; ++i)
            cin >> (arr[i] + 1);
        // cout << arr[r][c] << endl;
        // init();
        cin >> n;
        char op;
        printf("Case #%d:\n", i);
        while(n--)
        {
            cin >> op;
            if(op == 'M')
            {
                int x, y;
                char z;
                cin >> x >> y >> z;
                arr[x + 1][y + 1] = z;

            }
            else
                cout << init() << endl;
        }
    }
    return 0;
}